package at.studentinformation;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;


@SuppressWarnings("deprecation")
public class ServiceConnector {
	static String NAMESPACE = "http://vladozver.org/";    
    static String SOAP_ACTION = "http://vladozver.org/";
    static String URL = "http://10.0.2.2:8686/Service/MainService.asmx?WSDL";
    static AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
    
    public static String Request(String METHOD_NAME, ArrayList<PropertyInfo> Properties)
    {    	
    	SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    	for(int i = 0; i<Properties.size(); i++)
    		Request.addProperty(Properties.get(i));
    
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(Request);
	    
	    String result="";
	    try
	    {
	        androidHttpTransport.call(SOAP_ACTION + METHOD_NAME, envelope);
	        result = envelope.getResponse().toString();	        
	    }
	    catch(Exception e)
	    {
	        e.printStackTrace();
	    }
	    return result;
    }
    
    public static boolean Login(String user, String password)
    {    	    	       
        ArrayList<PropertyInfo> Properties = new ArrayList<PropertyInfo>();
        
        PropertyInfo property = new PropertyInfo();        
        property.name = "user";
        property.setValue(user);
        Properties.add(property);
        property = new PropertyInfo();        
        property.name = "password";
        property.setValue(password);
        Properties.add(property);
        
        String returnStr = ServiceConnector.Request("Login", Properties);
        ReturnData returndata = ReturnData.ExamineFromJson(returnStr);
        
        if(returndata.RetCode==Common.Success)
        	return true;
        else
        	return false;
    }
    
    public static ArrayList<Point> GetPoint(String subjectids)
    {    	    	 
    	 ArrayList<PropertyInfo> Properties = new ArrayList<PropertyInfo>();
    	 
         PropertyInfo property = new PropertyInfo();         
         property.name = "user";
         property.setValue(Common.userName);
         Properties.add(property);
         
         property = new PropertyInfo();        
         property.name = "password";
         property.setValue(Common.passWord);
         Properties.add(property);
         
         property = new PropertyInfo();        
         property.name = "subjectids";
         property.setValue(subjectids);
         Properties.add(property);
         
         String returnStr = ServiceConnector.Request("GetPoint", Properties);
         ReturnData returndata = ReturnData.ExamineFromJson(returnStr);
         
         ArrayList<Point> pointList = new ArrayList<Point>(); 
        		 
         if(returndata.RetCode==Common.Success)
         {
        	 pointList = Point.GetListPointFromJson(returndata.Content);
         }
         return pointList;
    }
    
    public static ArrayList<Subject> GetSubjectByStudentId(String subjectids)
    {
    	ArrayList<PropertyInfo> Properties = new ArrayList<PropertyInfo>();
   	 
        PropertyInfo property = new PropertyInfo();         
        property.name = "user";
        property.setValue(Common.userName);
        Properties.add(property);
        
        property = new PropertyInfo();        
        property.name = "password";
        property.setValue(Common.passWord);
        Properties.add(property);
        
        property = new PropertyInfo();        
        property.name = "subjectids";
        property.setValue(subjectids);
        Properties.add(property);
        
        String returnStr = ServiceConnector.Request("GetSubjectByStudentId", Properties);
        ReturnData returndata = ReturnData.ExamineFromJson(returnStr);
        
        ArrayList<Subject> subjectList = new ArrayList<Subject>(); 
       		 
        if(returndata.RetCode==Common.Success)
        {
        	subjectList = Subject.GetListSubjectFromJson(returndata.Content);
        }
        return subjectList;
    }       

    public static ArrayList<Schedule> GetSchedule()
    {
    	ArrayList<PropertyInfo> Properties = new ArrayList<PropertyInfo>();
   	 
        PropertyInfo property = new PropertyInfo();         
        property.name = "user";
        property.setValue(Common.userName);
        Properties.add(property);
        
        property = new PropertyInfo();        
        property.name = "password";
        property.setValue(Common.passWord);
        Properties.add(property);               
        
        String returnStr = ServiceConnector.Request("GetSchedule", Properties);
        ReturnData returndata = ReturnData.ExamineFromJson(returnStr);
        
        ArrayList<Schedule> scheduleList = new ArrayList<Schedule>(); 
       		 
        if(returndata.RetCode==Common.Success)
        {
        	scheduleList = Schedule.GetListSubjectFromJson(returndata.Content);
        }
        return scheduleList;
    }

    public static ArrayList<News> GetNewsListByDate(String date)
    {
    	ArrayList<PropertyInfo> Properties = new ArrayList<PropertyInfo>();
   	 
        PropertyInfo property = new PropertyInfo();         
        property.name = "user";
        property.setValue(Common.userName);
        Properties.add(property);
        
        property = new PropertyInfo();        
        property.name = "password";
        property.setValue(Common.passWord);
        Properties.add(property);
        
        property = new PropertyInfo();        
        property.name = "date";
        property.setValue(date);
        Properties.add(property);
        
        String returnStr = ServiceConnector.Request("GetNewsListByDate", Properties);
        ReturnData returndata = ReturnData.ExamineFromJson(returnStr);
        
        ArrayList<News> newsList = new ArrayList<News>(); 
       		 
        if(returndata.RetCode==Common.Success)
        {
        	newsList = News.GetListSubjectFromJson(returndata.Content);
        }
        return newsList;
    }
    
    public static News GetNewsById(String id)
    {
    	ArrayList<PropertyInfo> Properties = new ArrayList<PropertyInfo>();
   	 
        PropertyInfo property = new PropertyInfo();         
        property.name = "user";
        property.setValue(Common.userName);
        Properties.add(property);
        
        property = new PropertyInfo();        
        property.name = "password";
        property.setValue(Common.passWord);
        Properties.add(property);
        
        property = new PropertyInfo();        
        property.name = "id";
        property.setValue(id);
        Properties.add(property);
        
        String returnStr = ServiceConnector.Request("GetNewsById", Properties);
        ReturnData returndata = ReturnData.ExamineFromJson(returnStr);
        
        News news = new News(); 
       		 
        if(returndata.RetCode==Common.Success)
        {
        	try
        	{
	        	JSONArray entries = new JSONArray("[" + returndata.Content + "]");
	        	JSONObject post = entries.getJSONObject(0);        	
	        	news = News.ExamineFromJsonObject(post);
        	}
        	catch(Exception ex)
        	{
        		ex.printStackTrace();
        	}        
        }
        return news;
    }
}